package com.resjz.milddog.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Base64;
import java.util.HashMap;

@RestController
@RequestMapping("/fallBack")
public class FallBackController {
    @RequestMapping(value = "code")
    public JSONObject deviceInfo(String code) {
        String APP_ID = "client";
        String APPSECRET = "secret";
        //设置变量 url与返回值其中url使用拼接带入参数APP_ID， APPSECRET
        String url = "http://192.168.2.18:9002/oauth/token";

        String result = "";

        //设置链接
        HttpGet http = new HttpGet(url);
        //设置链接参数与要求
        http.setHeader("Content-Type", "application/x-www-form-urlencoded");
        HashMap<String, String> params = new HashMap<>();

        params.put("grant_type", "authorization_code");
        params.put("code", code);

        try {
            // 创建HttpClientBuilder
            HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
            CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
            HttpPost httpPost = new HttpPost(url);
            //添加http头信息
            httpPost.addHeader("Authorization", "Basic " + Base64.getUrlEncoder().encodeToString((APP_ID + ":" + APPSECRET).getBytes()));
            MultipartEntityBuilder builder = MultipartEntityBuilder.create();
            params.forEach((k, v) -> {
                builder.addPart(k, new StringBody(v, ContentType.MULTIPART_FORM_DATA));
            });
            HttpEntity postEntity = builder.build();
            httpPost.setEntity(postEntity);
            HttpResponse httpResponse = null;
            HttpEntity entity = null;
            try {
                httpResponse = closeableHttpClient.execute(httpPost);
                entity = httpResponse.getEntity();
                if (entity != null) {
                    result = EntityUtils.toString(entity);
                }
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            // 关闭连接
            closeableHttpClient.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        JSONObject jsonObject = JSON.parseObject(result);
        return jsonObject;
    }
}
